<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
defined ('IN_AGORA') or die;

class TaskController extends AgoraController {
	
	var $mod_model;
	
    function __construct() {
	
		parent::__construct();
		
		if (!($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator'])) {
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::makeUrl(array()));			
		}
		
		$this->loadDefaultView();
		
		$allowed = $this->access_model->getAllowedForums($this->agora_user['id'], 'post_approve');

		$this->mod_model = & Model::getInstance('ModerationModel');
		$this->mod_model->_setAllowedForumsArr($allowed);
		$this->mod_model->_setAllowedForums();
		$this->mod_model->_setSuperAdmin($this->agora_user['is_superadmin']);
    }
	
	function show_posts() {

		$num_posts = $this->mod_model->countUnapproved();
		$per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
		$num_pages = ceil(($num_posts)/$per_page);
		$this->setPagination($num_pages);
		$page = Agora::getPage($num_pages);
		
		$result = $this->mod_model->getUnapproved($per_page, $page);
		
		$parser = & $this->helper('parser');
		$user_helper = & $this->helper('user');
		foreach ($result as $key => $post) {
			$result[$key]['message'] = $parser->parseMessage($post['message'], $post['hide_smilies']);
			$result[$key]['can_approve'] = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $post['forum_id'], 'post_approve'))) ? 1 : 0;
			$result[$key]['avatar'] = $user_helper->prepareUserAvatar(array('id' => $post['poster_id'], 'jos_id' => $post['jos_id'], 'use_avatar' => 1));
		}
		
		$this->view->assignRef('result', $result);
		$this->view->assignRef('page', $page);
		$this->view->template = 'mod_posts';
		
	}

	function show_topics() {

		$num_topics = $this->mod_model->countUnapproved(0);
		$per_page = intval($this->agora_user['disp_topics']) > 0 ? $this->agora_user['disp_topics'] : intval($this->agora_config['o_disp_topics_default']);
		$num_pages = ceil(($num_topics)/$per_page);
		$this->setPagination($num_pages);
		$page = Agora::getPage($num_pages);
		
		$result = $this->mod_model->getUnapproved($per_page, $page, 0);
		
		$parser = & $this->helper('parser');
		$user_helper = & $this->helper('user');
		foreach ($result as $key => $post) {
			$result[$key]['message'] = $parser->parseMessage($post['message'], $post['hide_smilies']);
			$result[$key]['can_approve'] = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $post['forum_id'], 'post_approve'))) ? 1 : 0;
			$result[$key]['avatar'] = $user_helper->prepareUserAvatar(array('id' => $post['poster_id'], 'jos_id' => $post['jos_id'], 'use_avatar' => 1));
		}

		$this->view->assignRef('result', $result);
		$this->view->assignRef('page', $page);
		$this->view->template = 'mod_topics';
		
	}
	
	function decline_post() {
		$post_id = Agora::getRequestVar('id');
		$curr_page = Agora::getRequestVar('pg',1);
		$post_count = Agora::getRequestVar('nm',0);
		$post_count = (int)$post_count ? (int)$post_count - 1 : 0;

		$post_model = & Model::getInstance('PostModel');
		$post = $post_model->load($post_id);
		$topic_model = & Model::getInstance('TopicModel');
		$topic = $topic_model->load($post['topic_id']);
		$can_approve = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $topic['forum_id'], 'post_approve'))) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}

		$this->mod_model->declinePost($post_id);
		
		$per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
		$num_pages = ceil(($this->mod_model->countUnapproved())/$per_page);
		$curr_page = $curr_page > $num_pages ? $num_pages : $curr_page;
		$fragment = $curr_page > $num_pages ? '' : "#p$post_count";
		$url = Agora::makeURL(array('task' => 'moderation', 'action' => 'show_posts', 'p' => $curr_page)) . $fragment;
		
		Agora::redirect($url);

	}
	
	function decline_topic() {
		$topic_id = Agora::getRequestVar('id');
		$curr_page = Agora::getRequestVar('pg',1);
		$topic_count = Agora::getRequestVar('nm',0);
		$topic_count = (int)$topic_count ? (int)$topic_count - 1 : 0;

		$topic_model = & Model::getInstance('TopicModel');
		$topic = $topic_model->load($topic_id);
		$can_approve = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $topic['forum_id'], 'post_approve'))) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}

		$this->mod_model->declineTopic($topic_id);

		$per_page = intval($this->agora_user['disp_topics']) > 0 ? $this->agora_user['disp_topics'] : intval($this->agora_config['o_disp_topics_default']);
		$num_pages = ceil(($this->mod_model->countUnapproved(0))/$per_page);
		$curr_page = $curr_page > $num_pages ? $num_pages : $curr_page;
		$fragment = $curr_page > $num_pages ? '' : "#p$topic_count";
		$url = Agora::makeURL(array('task' => 'moderation', 'action' => 'show_topics', 'p' => $curr_page)) . $fragment;
		
		Agora::redirect($url);

	}

	function approve_post() {

		$post_model = & Model::getInstance('PostModel');
		$post_id = Agora::getRequestVar('id');
		$post = $post_model->load($post_id);

		$topic_model = & Model::getInstance('TopicModel');
		$topic = $topic_model->load($post['topic_id']);
		
		$curr_page = Agora::getRequestVar('pg',1);
		$post_count = Agora::getRequestVar('nm',0);
		$post_count = (int)$post_count ? (int)$post_count - 1 : 0;
		
		$can_approve = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $topic['forum_id'], 'post_approve'))) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}

		// Approve the post
		$this->mod_model->approvePost($post_id, $post['topic_id'], $topic['forum_id'], $post['poster_id'], $post['poster']);
		
		$parser = & $this->helper('parser');
		$message = $parser->fix_broken_bbcode($post['message']);
		
		// Trigger onBeforeReply Agora plugin event
		JPluginHelper::importPlugin('agora');
		$dispatcher =& JDispatcher::getInstance();
		$options = new JObject;
		$options->forum_id = $topic['forum_id'] ;
		$options->subject = $topic['subject'] ;
		$options->user_name = ($topic['poster'] ? $topic['poster'] : $this->agora_user['username'] );
		$options->user_id = ($topic['poster_id'] ? $topic['poster_id'] : $this->agora_user['id'] );
		$options->message = $message ;
		$options->topic_id = $post['topic_id'];
		$plg_result = $dispatcher->trigger('onBeforeReply', array(&$options));
		
		// Trigger onAfterReply Agora plugin event
		$options->post_id = $post_id;
		$plg_result = $dispatcher->trigger('onAfterReply', array(&$options)) ;	
		
		// If AUP plugin exists - add user points
		$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
		if (file_exists($api_AUP))	{
			require_once ($api_AUP);
			AlphaUserPointsHelper::newpoints( 'plgaup_reply_agora', '', '', Agora::lang('AUP_REPLY') );
		}

		// Send notification to subscribed users
		$subscription_helper = &$this->helper('subscription');
		$subscription_helper->newPost($post_id, $topic['forum_id']);

		// Build redirect url
		$per_page = intval($this->agora_user['posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
		$num_pages = ceil(($this->mod_model->countUnapproved())/$per_page);
		$curr_page = $curr_page > $num_pages ? $num_pages : $curr_page;
		$fragment = $curr_page > $num_pages ? '' : "#p$post_count";
		$url = Agora::makeURL(array('task' => 'moderation', 'action' => 'show_posts', 'p' => $curr_page)) . $fragment;
		
		Agora::redirect($url);
	}
	
	function approve_topic() {
	
		$topic_id = Agora::getRequestVar('id');
		$post_id = Agora::getRequestVar('post_id');

		$topic_model = & Model::getInstance('TopicModel');
		$topic = $topic_model->load($topic_id);

		$post_model = & Model::getInstance('PostModel');
		$post = $post_model->load($post_id);

		$curr_page = Agora::getRequestVar('pg',1);
		$topic_count = Agora::getRequestVar('nm',0);
		$topic_count = (int)$topic_count ? (int)$topic_count - 1 : 0;

		$can_approve = ($this->agora_user['is_superadmin'] || (($this->agora_user['is_admin'] || $this->agora_user['is_moderator']) && $this->access_model->authenticate($this->agora_user['id'], $topic['forum_id'], 'post_approve'))) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}
		
		// approve the topic
		$this->mod_model->approveTopic($topic['forum_id'], $topic_id, $post_id, $post['poster_id'], $post['poster']);
		
		$parser = & $this->helper('parser');
		$message = $parser->fix_broken_bbcode($post['message']);

		// Trigger onBeforeNewTopic Agora plugin event
		JPluginHelper::importPlugin('agora');
		$dispatcher =& JDispatcher::getInstance();
		$options = new JObject;
		$options->forum_id = $topic['forum_id'];
		$options->subject = $topic['subject'];
		$options->desc = $topic['descrip_t'];
		$options->icon = $topic['icon_topic'];
		$options->user_name = ($post['poster'] ? $post['poster'] : $this->agora_user['username'] );
		$options->user_id = ($post['poster_id'] ? $post['poster_id'] : $this->agora_user['id'] );
		$options->message = $message;
		$options->topic_id = null;
		$plg_result =  $dispatcher->trigger('onBeforeNewTopic', array(&$options));
		
		// Trigger onBeforeCreatePoll Agora plugin event
		if ($topic['question']) {
			$poll = $options;
			$poll->question = $topic['question'];
			$poll->poll_yes = $topic['yes'];
			$poll->poll_no = $topic['no'];
			$plg_result = $dispatcher->trigger('onBeforeCreatePoll', array(&$poll));
		}
		
		// Trigger onAfterNewTopic Agora plugin event
		$options->topic_id 	= $topic_id;
		$plg_result = $dispatcher->trigger('onAfterNewTopic', array(&$options));
		
		// Trigger onAfterCreatePoll Agora plugin event
		if ($topic['question']) {
			$poll->topic_id = $topic_id;
			$plg_result = $dispatcher->trigger('onAfterCreatePoll', array(&$poll));
		}
		
		// Agora auto article
		JPluginHelper::importPlugin( 'agora', 'agoraautoarticle');
		$plugin =& JPluginHelper::getPlugin('agora', 'agoraautoarticle');
		$plugin_params = (is_object($plugin) && property_exists($plugin, 'params')) ? $plugin->params : null;
		$pluginParams = new JParameter( $plugin_params );
		if ($pluginParams->get('plg_mode', '')) {
			$dispatcher =& JDispatcher::getInstance();
			$params = array($topic['forum_id'], $topic_id, $topic['subject'], $topic['descrip_t'], $message, $post['poster']);
			$dispatcher->trigger( 'onAutoArticle', $params);
		}
		
		// If AUP plugin exists - add user points
		$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
		if (file_exists($api_AUP))	{
			require_once ($api_AUP);
			AlphaUserPointsHelper::newpoints( 'plgaup_newtopic_agora', '', '', Agora::lang('AUP_TOPIC'));
		}
			
		// Send notification to subscribed users
		$subscription_helper = & $this->helper('subscription');
		$subscription_helper->newPost($topic['last_post_id'], $topic_id);
		
		// Build redirect url
		$per_page = intval($this->agora_user['disp_topics']) > 0 ? $this->agora_user['disp_topics'] : intval($this->agora_config['o_disp_topics_default']);
		$num_pages = ceil(($this->mod_model->countUnapproved(0))/$per_page);
		$curr_page = $curr_page > $num_pages ? $num_pages : $curr_page;
		$fragment = $curr_page > $num_pages ? '' : "#t$topic_count";
		$url = Agora::makeURL(array('task' => 'moderation', 'action' => 'show_topics', 'p' => $curr_page)) . $fragment;
		
		Agora::redirect($url);
	}
	
	function get_content() {
		$post_id = (int)Agora::getRequestVar('id');
		$result = $this->mod_model->getPostContent($post_id);
		$parser = & $this->helper('parser');
		$result = $parser->parseMessage($result, 0);
		echo $result;
		exit;
	}
	
	function approve_all_posts() {
	
		$can_approve = ($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator']) ? 1 : 0;
		if (!$can_approve) {
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}

		$parser = & $this->helper('parser');
		$subscription_helper = & $this->helper('subscription');
		
		// Agora plugins
		JPluginHelper::importPlugin('agora');
		$dispatcher =& JDispatcher::getInstance();
		JPluginHelper::importPlugin( 'agora', 'agoraautoarticle');
	
		// Alpha user points
		$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
		if (file_exists($api_AUP))	{
			require_once ($api_AUP);
		}
		
		$posts = $this->mod_model->getPosts();
		
		foreach ($posts as $post) {
		
			// approve the post
			$this->mod_model->approvePost($post['id'], $post['topic_id'], $post['forum_id'], $post['poster_id'], $post['poster']);
			
			$message = $parser->fix_broken_bbcode($post['message']);

			// Trigger onBeforeReply Agora plugin event
			$options = new JObject;
			$options->forum_id = $post['forum_id'] ;
			$options->subject = $post['subject'] ;
			$options->user_name = ($post['poster'] ? $post['poster'] : $this->agora_user['username'] );
			$options->user_id = ($post['poster_id'] ? $post['poster_id'] : $this->agora_user['id'] );
			$options->message = $message ;
			$options->topic_id = $post['topic_id'];
			$plg_result = $dispatcher->trigger('onBeforeReply', array(&$options));

			// Trigger onAfterReply Agora plugin event
			$options->post_id = $post['id'];
			$plg_result = $dispatcher->trigger('onAfterReply', array(&$options)) ;

			// If AUP plugin exists - add user points
			if (file_exists($api_AUP))	{
				AlphaUserPointsHelper::newpoints( 'plgaup_reply_agora', '', '', Agora::lang('AUP_REPLY') );
			}
			
			// Send notification to subscribed users
			$subscription_helper->newPost($post['id'], $post['forum_id']);
		}
		
		Agora::redirect(Agora::makeURL(array('task' => 'moderation', 'action' => 'show_posts')));
	}
	
	function approve_all_topics() {

		$can_approve = ($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator']) ? 1 : 0;
		if (!$can_approve) {
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}

		$parser = & $this->helper('parser');
		$subscription_helper = & $this->helper('subscription');
		
		// Agora plugins
		JPluginHelper::importPlugin('agora');
		$dispatcher =& JDispatcher::getInstance();
		JPluginHelper::importPlugin( 'agora', 'agoraautoarticle');
		//Agora Autoarticle
		$plugin =& JPluginHelper::getPlugin('agora', 'agoraautoarticle');		
		$plugin_params = (is_object($plugin) && property_exists($plugin, 'params')) ? $plugin->params : null;
		$pluginParams = new JParameter( $plugin_params );
		if ($pluginParams->get('plg_mode', '')) {
			$dispatcher1 =& JDispatcher::getInstance();
		}		
		// Alpha user points
		$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
		if (file_exists($api_AUP))	{
			require_once ($api_AUP);
		}
		
		$topics = $this->mod_model->getTopics();
		
		foreach ($topics as $topic) {
		
			// approve the topic
			$this->mod_model->approveTopic($topic['forum_id'], $topic['id'], $topic['post_id'], $topic['poster_id'], $topic['poster']);

			$message = $parser->fix_broken_bbcode($topic['message']);

			// Trigger onBeforeNewTopic Agora plugin event
			$options = new JObject;
			$options->forum_id = $topic['forum_id'];
			$options->subject = $topic['subject'];
			$options->desc = $topic['descrip_t'];
			$options->icon = $topic['icon_topic'];
			$options->user_name = ($topic['poster'] ? $topic['poster'] : $this->agora_user['username'] );
			$options->user_id = ($topic['poster_id'] ? $topic['poster_id'] : $this->agora_user['id'] );
			$options->message = $message;
			$options->topic_id = null;
			$plg_result =  $dispatcher->trigger( 'onBeforeNewTopic', array(&$options));

			// Trigger onBeforeCreatePoll Agora plugin event
			if ($topic['question']) {
				$poll = $options;
				$poll->question = $topic['question'];
				$poll->poll_yes = $topic['yes'];
				$poll->poll_no = $topic['no'];
				$plg_result = $dispatcher->trigger('onBeforeCreatePoll', array(&$poll));
			}
		
			// Trigger onAfterNewTopic Agora plugin event
			$options->topic_id 	= $topic['id'];
			$plg_result = $dispatcher->trigger( 'onAfterNewTopic', array(&$options));

			// Trigger onAfterCreatePoll Agora plugin event
			if ($topic['question']) {
				$poll->topic_id = $topic['id'];
				$plg_result = $dispatcher->trigger('onAfterCreatePoll', array(&$poll));
			}

			// Agora auto article
			if ($pluginParams->get('plg_mode', '')) {
				$dispatcher->trigger('onAutoArticle', array($topic['forum_id'], $topic['id'], $topic['subject'], $topic['descrip_t'], $message, $topic['poster']));
			}
		
			// If AUP plugin exists - add user points
			if (file_exists($api_AUP))	{
				AlphaUserPointsHelper::newpoints( 'plgaup_newtopic_agora', '', '', Agora::lang('AUP_TOPIC'));
			}
			
			// Send notification to subscribed users
			$subscription_helper->newPost($topic['last_post_id'], $topic['id']);
		}
		
		Agora::redirect(Agora::makeURL(array('task' => 'moderation', 'action' => 'show_topics')));		
	}
	
	function decline_all_posts() {
		$can_approve = ($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator']) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}
		$this->mod_model->declineAllPosts();
		Agora::redirect(Agora::makeURL(array('task' => 'moderation', 'action' => 'show_posts')));
	}

	function decline_all_topics() {
		$can_approve = ($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator']) ? 1 : 0;
		if (!$can_approve){
			Agora::showMessage(Agora::lang('Access denied'));
			Agora::redirect(Agora::getRefferer());
		}
		$this->mod_model->declineAllTopics();
		Agora::redirect(Agora::makeURL(array('task' => 'moderation', 'action' => 'show_topics')));
	}


}

?>